// pages/login/login.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dialogShow:false,
    buttons:[{text:'取消'},{text:'确定'}],
    userId:'',
    token:'',
    roleId:'',
    roleName:'',
    name:''

  },
  login(){
    console.log("点击登录！")
  },
  getPhoneNumber (e) {
    //console.log(app.globalData.sessionKey)
    //console.log(e)
    //console.log(e.detail.encryptedData)
    //console.log(e.detail.iv)
    wx.request({
      url: 'http://localhost:9999/mini/login',
      method:'POST',
      data:{
        encryptedData:e.detail.encryptedData,
        iv:e.detail.iv,
        sessionKey:app.globalData.sessionKey
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success:res =>{
        console.log(res)
        this.setData({
          userId:res.data.result.userInfo.id,
          token:res.data.result.token,
          name:res.data.result.userInfo.realname

        })
        //设置用户id
        wx.setStorageSync('userId', this.data.userId)
        wx.setStorageSync('name', this.data.name)
        //获取该用户的角色id
        this.getUserRoleById(this.data.userId)
        
      }
    })
  },
  getUserRoleById(id){
    //获取角色id
    wx.request({
      url: 'http://localhost:9999/sys/user/queryUserRole',
      method:'GET',
      data:{
        userid:id
      },
      header:{
        'context-type': 'application/json',
        'token':this.data.token
      },
      success:res =>{
        console.log(res)
        this.getRoleById(res.data.result[0])
      }
    })
  },
  getRoleById(id){
    wx.request({
      url: 'http://localhost:9999/sys/role/queryById',
      method:'GET',
      data:{
        id:id
      },
      success:res =>{
        console.log(res)
        this.setData({
          roleName:res.data.result.roleName
        })
        if(this.data.roleName === '厨房用户'){
          wx.setStorageSync('role', 1)
        }else{
          wx.setStorageSync('role', 0)
        }
        wx.reLaunch({
          url: '/pages/my/my',
        })
        
      }
    })
  },
  tapOneDialogButton:function(){
    this.setData({
      dialogShow:true
    })
  },
  tapDialogButton(e) {
    this.setData({
        dialogShow: false,
        showOneButtonDialog: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})